home *** CD-ROM | disk | FTP | other *** search
- Path: news.infi.net!usenet
- From: nngis@norfolk.infi.net (Greg DiGiorgio)
- Newsgroups: comp.lang.c
- Subject: Re: File Search Algorithm
- Date: 12 Jan 1996 15:40:37 GMT
- Organization: Customer of InfiNet
- Message-ID: <4d5vdl$5g6@news.infi.net>
- References: <1996Jan11.044752.6290@leo.vsla.edu>
- Reply-To: nngis@norfolk.infi.net
- NNTP-Posting-Host: h-brahe.norfolk.infi.net
- Mime-Version: 1.0
- X-Newsreader: WinVN 0.99.3
-
- In article <1996Jan11.044752.6290@leo.vsla.edu>, tbrown@leo.vsla.edu
- says...
- >
- >Does anyone have an algorithm for a file search? One that
- >finds a file across multiple directories? OS-Generic code
- >would be welcome, but MS-DOS specific code would be better.
- >
- > Sincerely,
- > Todd Brown
- >--
- >Internet: tbrown@pobox.com
- >Snail mail: Todd Brown
- > Star Route, Box 452
- > Lottsburg, VA 22511
-
- Todd, here's a solution. It uses the "brute-force" method of string
- searching and shows how you could code your own "strstr" function.
-
- /************************************************************************
- ****
- SEARCH.C
- *************************************************************************
- ****
- This program acts as a file search utility by searching a file
- line-by-line for a specified string of characters.
-
- Ex: To search a file called JUNK.C for a string 'while', you
- would enter:
-
- SEARCH JUNK.C while
- *************************************************************************
- ****/
- #include <stdio.h>
- #include <string.h>
- #include <stdlib.h>
-
- /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= GLOBAL VARIABLES
- =-=-=-=-=-=-=-=-=-=-=-=*/
-
- FILE *infile;
- char filename[80], /* input file name */
- file_str[133], /* holds line read from the file */
- sub_str[80]; /* holds the substring to search for in file_str
- */
-
-
- /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= FUNCTIONS
- =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
- getline - This function automatically opens a file, reads a line
- from it, returns that line to the calling function, and
- closes the file, exitting from the program when it hits
- EOF.
-
- It accepts one input, the variable that will hold the
- line
- read in. It loads the string variable, "inpline" with
- the
- line read.
- =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
- -=*/
-
- void getline(char inpline[])
- {
- static file_open = 0;
- if (!file_open) {
- infile = fopen(filename,"r");
- if (infile == NULL) {
- printf("Can not open file %s\n",filename);
- exit(99);
- }
- }
- file_open = 1;
- if (fgets(inpline, 132, infile) == NULL) {
- printf("\nEOF reached; program terminating...\n");
- exit(0);
- }
- }
-
- int find ( char file_str[], char sub_str[] )
- {
-
- /*********************************************************************
- This function accepts 2 strings a input:
-
- file_str - this is the line that you read from the file.
- sub_str - this is the string that you are searching for.
-
- Implementing a BRUTE FORCE string search method, this function
- returns
- a one if 'sub_str' can be found in 'file_str'; else zero. Also,
- this
- function only checks for the first occurence of 'sub_str' in
- 'file_str'.
-
- *********************************************************************/
-
- /*********************** Your Code Goes Here
- ***********************/
- int found;
- int current,substr,searchstr;
-
- found=0; current=0;
- while (current<strlen(file_str)) {
- if (sub_str[0]==file_str[current]) {
- if ( (strlen(file_str)-current)<strlen(sub_str))
- break;
- substr=0; searchstr=current;
- while (substr < strlen(sub_str) &&
- sub_str[substr]==file_str[searchstr] ){
- substr++; searchstr++;
- }
- if (substr == strlen(sub_str)) {
- found=1;
- break;
- }
- }
- current++;
- }
- return(found);
- }
-
- main(int argc, char *argv[])
- {
- strcpy(filename,argv[1]); strcpy(sub_str, argv[2]);
- do {
- getline(file_str); /* read a line from the file */
- if (find(file_str,sub_str))
- printf("%s",file_str);
- }
- while (1);
- }
-
-